package android.money;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.money.data.Card;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.Toast;

public class AccountEditor extends Activity{
	private final String TXT_INVALID_TIME = "Invalid expire time";
	protected EditText cardNumber ;
	protected EditText secCode ;
	protected EditText accName ;
	protected EditText holderName;
	protected Spinner typeSpinner ;
	protected Spinner expireMonthSpinner;
	protected Spinner expireYearSpinner ;
	protected Button submitButton;
	protected Button deleteButton;
	protected TableRow buttonSet;
	private void initComponent(){
		setContentView(R.layout.account_editor_page);	     
		cardNumber = (EditText)findViewById(R.id.cardNo);
		secCode = (EditText)findViewById(R.id.secCode);
		accName = (EditText)findViewById(R.id.accName);	
		holderName = (EditText)findViewById(R.id.holderName);	
		typeSpinner = (Spinner)findViewById(R.id.typeSpinner);
		expireMonthSpinner =(Spinner)findViewById(R.id.monthExpireSpinner);
		expireYearSpinner =(Spinner)findViewById(R.id.yearExpireSpinner);
		buttonSet = (TableRow)findViewById(R.id.accountButtonRow);
		deleteButton = (Button)findViewById(R.id.deleteAccountButton);
		submitButton = (Button)findViewById(R.id.submitAcccountButton);
		List<String> typeList = Card.getTypeList();
		ArrayAdapter<String> adapterTypeList = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,typeList);
		adapterTypeList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		typeSpinner.setAdapter(adapterTypeList);

		List<String> monthList = getMonthList();
		ArrayAdapter<String> adapterMonthList = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,monthList);
		adapterMonthList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		expireMonthSpinner.setAdapter(adapterMonthList);
		
		List<String> yearList = getYearList();
		ArrayAdapter<String> adapteryearList = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,yearList);
		adapteryearList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		expireYearSpinner.setAdapter(adapteryearList);	  
	}
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		initComponent();
	}
	protected List<String>getYearList(){
		int offsetSize = 10;
		List<String> yList = new ArrayList<String>(offsetSize);
		Calendar calendar = Calendar.getInstance();
		for(int offset = 0 ; offset < offsetSize ; offset++)
			yList.add((calendar.get(Calendar.YEAR)+offset)+"");
		return yList;
	}
	protected List<String> getMonthList(){
		List<String> mList = new ArrayList<String>(12);
		for(int month = 1 ; month <= 12 ; month++)
			mList.add(month+"");
		return mList;
	}
	protected boolean wasExpired(int expYear , int expMonth) {
		Calendar calendar = Calendar.getInstance();
		int thisYear = calendar.get(Calendar.YEAR);
		int thisMonth = calendar.get(Calendar.MONTH);
		return thisYear > expYear ||(thisYear == expYear  && thisMonth > expMonth);
	}
	protected boolean isValidData(){
		short expMonth = Short.parseShort((String)expireMonthSpinner.getSelectedItem());
		short expYear = Short.parseShort((String)expireYearSpinner.getSelectedItem());
		if(wasExpired(expYear, expMonth))
		{
			Toast.makeText(this, TXT_INVALID_TIME, Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
}
